//
//  HomeTableViewController.swift
//  HMWeibo007
//
//  Created by TTang on 15/6/28.
//  Copyright © 2015年 shanyu. All rights reserved.
//

import UIKit
import SVProgressHUD
class HomeTableViewController: BaseTableViewController {

    /// 表格绑定的微博数据数组
    var statusesList: [Status]? {
        didSet{
            //刷新表格
            tableView.reloadData()
        }
    }
    
    // 定义行高缓存 [statusId: 行高]
    lazy var rowHeightCache = [Int: CGFloat]()
    
    override func viewDidLoad() {
        super.viewDidLoad()

        visitiorView?.setVisitiorViewInfo("关注一些人，回这里看看有什么惊喜", iconName: "visitordiscover_feed_image_house", isHome: true)
    
        setUpNavationBar()
        
        loadData()
        //注册可重用cell
        tableView.registerClass(StatusCell.self, forCellReuseIdentifier: "Cell")
        
        // 取消分隔线
        tableView.separatorStyle = UITableViewCellSeparatorStyle.None
    }

    ///  加载微博数据的方法
    private func loadData(){
       
       Status.loadData { (statuses, error) -> () in
        
        if error != nil {
           
            // 不要把错误的相信信息告诉用户，程序员应该多测试！
            SVProgressHUD.showInfoWithStatus("您的网络不给力！")
            return
        
        }
         // 提示，后续会有下拉刷新，没有新微博的情况，不要抱错！
        if statuses == nil {
            
            print("没有数据")
            return
        }
        
            self.statusesList = statuses
        }
        
    }
    
    // MARK: - 表格的数据源方法
    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        
        return self.statusesList?.count ?? 0
    }
    
    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
         //此方法必须注册可重用cell。如果不注册，会崩溃
         let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! StatusCell
         //设置cell
         cell.status = statusesList![indexPath.row]
        
         return cell
    }
    
    override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
        
        //1.取到对象
        let status = statusesList![indexPath.row]
         // 1.1 判断是否缓存了行高，如果有直接返回
        if rowHeightCache[status.id] != nil{
            
            print("返回缓存行高")
            return rowHeightCache[status.id]!
        }
        
        
        //2.获取cell
        let cell = tableView.dequeueReusableCellWithIdentifier("Cell") as! StatusCell
        //3.返回行高
        let height = cell.rowHeight(status)
        rowHeightCache[status.id] = height
        return height
    }
    
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        //启动动画
        visitiorView?.startAnimation()
    }
    
    /// 动画转场代理
    let popoverAnimator = PopoverAnimator()
    
    func titleBtnClicked(){
    
        let sb = UIStoryboard(name: "FriendGroup", bundle: nil)
        let vc = sb.instantiateViewControllerWithIdentifier("FriendGroupSB")
        
        //1.设置转场代理
        vc.transitioningDelegate  = popoverAnimator
        //2. 设置视图的展现大小
        let x = (view.bounds.width - 200) * 0.5
        popoverAnimator.presentFrame = CGRectMake(x, 56, 200, 240)
        
        //3.设置转场模式 - 自定义动画
        vc.modalPresentationStyle = UIModalPresentationStyle.Custom
      
        
        presentViewController(vc, animated: true, completion: nil)
    }
    
    ///  显示二维码界面
    func showQRCode() {
        let sb = UIStoryboard(name: "QRCode", bundle: nil)
        presentViewController(sb.instantiateInitialViewController()!, animated: true, completion: nil)
    }
    
    ///  设置导航栏
    private func setUpNavationBar(){
    
        if sharedUserAccount == nil{
           return
        }
    
        //1.设置两侧按钮
      
        navigationItem.leftBarButtonItem = UIBarButtonItem(imageName: "navigationbar_friendsearch", highlightImageName: nil)
        navigationItem.rightBarButtonItem = UIBarButtonItem(imageName: "navigationbar_pop", highlightImageName: nil)
        
        // 1.1 获取 button
        let btn = navigationItem.rightBarButtonItem!.customView as! UIButton
        // 1.2 添加 target
        btn.addTarget(self, action: "showQRCode", forControlEvents: UIControlEvents.TouchUpInside)
        
        //2.设置标题按钮
        let titleBtn = HomeTitleButton.button(sharedUserAccount!.name!)
        
        titleBtn.addTarget(self, action: "titleBtnClicked", forControlEvents: UIControlEvents.TouchUpInside)
        
        navigationItem.titleView = titleBtn
        
        
    }
    
}
       


